package com.example.treasureadmin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.treasureadmin.entity.MsgEntity;
import com.example.treasureadmin.service.OrderService;
import com.example.treasureadmin.model.OrderModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/treasure-admin")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/order/all")
    public MsgEntity getAllOrders(){
        List<OrderModel> all = orderService.getAll();
        return new MsgEntity("200","SUCCESS",all);
    }

    @GetMapping("/order/status/{status}")
    public MsgEntity getStateOrders(@PathVariable("status") Integer status){
        List<OrderModel> all = orderService.getStatusOrders(status);
        return new MsgEntity("200","SUCCESS",all);
    }

    @PostMapping("/order/{id}")
    public MsgEntity sendGoods(@PathVariable("id") Integer id){
        orderService.sendGoods(id);
        return new MsgEntity("200","SUCCESS","发货成功");
    }

    @PutMapping("order/{id}")
    public MsgEntity changeAddr(@PathVariable("id") Integer id,@RequestBody JSONObject requestBody){
        String province = requestBody.getString("province");
        String city = requestBody.getString("city");
        String addr = requestBody.getString("addr");
        String name = requestBody.getString("name");
        String phone = requestBody.getString("phone");
        orderService.changeAddr(id,province,city,addr,name,phone);
        return new MsgEntity("200","SUCCESS","修改成功");
    }

    @GetMapping("/order/{id}")
    public MsgEntity getIdOrders(@PathVariable("id") Integer id){
        OrderModel all = orderService.getIdOrders(id);
        return new MsgEntity("200","SUCCESS",all);
    }
//
//    @PostMapping("/order/add")
//    public MsgEntity addOrder(@RequestBody JSONObject requestBody)
//    {
//        String goodsName = requestBody.getString("goods_name");
//        String goodsImage = requestBody.getString("goods_image");
//        String email = requestBody.getString("email");
//        String province = requestBody.getString("province");
//        String city = requestBody.getString("city");
//        String addr = requestBody.getString("addr");
//        String name = requestBody.getString("name");
//        String phone = requestBody.getString("phone");
//        orderService.addOrder(goodsName,goodsImage,email,province,city,addr,name,phone);
//        return new MsgEntity("200","SUCCESS","添加成功");
//    }

    @DeleteMapping("/order/{id}")
    public MsgEntity deleteOrder(@PathVariable("id") Integer id)
    {
        String code = orderService.deleteOrder(id);
        if(code.equals("200"))
            return new MsgEntity("200","SUCCESS","修改成功");
        return new MsgEntity("200","SUCCESS","订单不存在");
    }
}
